package source;

public class Main {

	String[] c = new String[5];
	int[] v = new int[5];
	int iv = -1;
	int ic = 0;

	String str = "12+8-567";

	public static void main(String[] args) {

		Main m = new Main();
		Double kq = m.XuLi();
		System.out.println("\n" + kq);
		// System.out.println(m.deg("*"));
		// System.out.println(m.isNumber("12+45"));
	}

	public String[] parseString(String str) {
		char[] strChar = str.toCharArray();
		String[] result = new String[strChar.length];
		for (int i = 0; i < result.length; i++)
			result[i] = "";

		int i = 0, j = 0;
		int u = -1;

		while (i < strChar.length) {

			if (Character.isDigit(strChar[i])) {
				u++;
				result[u] = result[u] + strChar[i];
				j = i + 1;
				while (j < strChar.length && Character.isDigit(strChar[j])) {
					result[u] = result[u] + strChar[j];
					i = j;
					j++;
				}
			} else {
				if (strChar[i] == '+') {
					u++;
					result[u] = result[u] + strChar[i];
				}
				if (strChar[i] == '-') {
					u++;
					result[u] = result[u] + strChar[i];
				}
			}
			i++;
		}
		return result;
	}

	public Double XuLi() {
		String[] strMath1 = parseString(str);
		int y = 0;
		for (int l = 0; l < strMath1.length; l++)
			if (strMath1[l].equals(""))
				y = l;
		ic = 0;
		for (int i = 0; i < 5; i++) {
			c[i] = "";
			v[i] = 0;
		}

		String[] strMath = new String[y];
		for (int z = 0; z < strMath.length; z++)
			strMath[z] = strMath1[z];
		c[ic] = "#";
		for (int i = 0; i < strMath.length; i++) {
			if (isNumber(strMath[i])) {
				iv++;
				v[iv] = Integer.parseInt(strMath[i]);
			}
			if (isOpt(strMath[i]))
				calOpt(strMath[i]);
		}

		calculate(c[ic]);
		ic--;

		return Double.parseDouble(Integer.toString(v[iv]));
	}

	public boolean isNumber(String n) {
		for (int i = 0; i < n.length(); i++) {
			char charc = n.charAt(i);
			if (!Character.isDigit(charc))
				return false;
		}
		return true;
	}

	public int deg(String pt) {
		if (pt.equals("+") | pt.equals("-"))
			return 1;
		else if (pt.equals("*") | pt.equals("/"))
			return 2;
		else
			return 0;
	}

	public boolean isOpt(String pt) {
		if (pt.equals("+") | pt.equals("-"))
			return true;
		else
			return false;
	}

	private void calOpt(String pt) {
		if (deg(c[ic]) >= deg(pt)) {
			calculate(c[ic]);
			--ic;
		}
		c[++ic] = pt;
	}

	private void calculate(String pt) {
		switch (pt) {
		case "+":
			v[iv - 1] = v[iv - 1] + v[iv];
			iv = iv - 1;
			break;

		case "-":
			v[iv - 1] = v[iv - 1] - v[iv];
			iv = iv - 1;
			break;
		}
	}
}
